home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Tools / HexEdit 1.0.7 ƒ / HexEditSource / Source / EditScrollBar.c < prev    next >
Text File  |  1993-12-11  |  5KB  |  212 lines

  1. /*********************************************************************
  2.  * EditScrollBar.c
  3.  *
  4.  * HexEdit, a simple hex editor
  5.  * copyright 1993, Jim Bumgardner
  6.  *********************************************************************/
  7. #include "HexEdit.h"
  8.  
  9. void HEditScrollToPosition(EditWindowPtr dWin, long newPos);
  10. void AdjustScrollBars(WindowPtr theWin, short resizeFlag);
  11.  
  12. void SetupScrollBars(EditWindowPtr dWin)
  13. {
  14.     Rect    sRect,r;
  15.     // !! Set up vertical scroll bar
  16.     //
  17.     r = ((WindowPtr) dWin)->portRect;
  18.     sRect.left = r.right - (SBarSize - 1);
  19.     sRect.top = r.top - 1;
  20.     sRect.right = r.right + 1;
  21.     sRect.bottom = r.bottom  - GrowIconSize;
  22.      dWin->vScrollBar = NewControl((WindowPtr) dWin,&sRect,"\p",true,
  23.                                  0,0,sRect.bottom - sRect.top,scrollBarProc,1L);
  24.     AdjustScrollBars((WindowPtr) dWin, 1);
  25. }
  26.  
  27. //
  28. // Adjust scroll bars when they need to be redrawn for some reason.
  29. //
  30. // resizeFlag is an optimization to avoid extra work when you aren't
  31. // resizing.
  32. //
  33.  
  34. void AdjustScrollBars(WindowPtr theWin, short resizeFlag)
  35. {
  36.     short            w,h;
  37.     GrafPtr            savePort;
  38.     EditWindowPtr    dWin = (EditWindowPtr) theWin;
  39.     long            limit;
  40.  
  41.     GetPort(&savePort);
  42.  
  43.     if (resizeFlag) {
  44.         // Adjust Lines Per Page
  45.         dWin->linesPerPage = ((theWin->portRect.bottom - SBarSize) - HeaderHeight - TopMargin - BotMargin)/LineHeight;
  46.  
  47.         // Move sliders to new position
  48.         //
  49.         MoveControl(dWin->vScrollBar, theWin->portRect.right - (SBarSize - 1),
  50.                                             theWin->portRect.top - 1);    
  51.         // Change their sizes to fit new window dimensions
  52.         //
  53.         w = 16;
  54.         h = theWin->portRect.bottom - theWin->portRect.top - (GrowIconSize - 1);
  55.         SizeControl(dWin->vScrollBar,w,h);
  56.  
  57.         // Reset their maximum values
  58.         //
  59.         SetCtlMax(dWin->vScrollBar,h);
  60.     }
  61.  
  62.     // Reposition painting if you have resized or scrolled past the legal
  63.     // bounds  Note: this call will usually be followed by an update
  64.     //
  65.     limit = ((dWin->fileSize+15) & 0xFFFFFFF0) - (dWin->linesPerPage << 4);
  66.     if (dWin->editOffset > limit)
  67.         dWin->editOffset = limit;
  68.     if (dWin->editOffset < 0)
  69.         dWin->editOffset = 0;
  70.  
  71.     // Set the value of the sliders accordingly
  72.     //
  73.     h = theWin->portRect.bottom - theWin->portRect.top - (GrowIconSize - 1);
  74.     if (limit > 0) {
  75.         h = theWin->portRect.bottom - theWin->portRect.top - (GrowIconSize - 1);
  76.         SetCtlMax(dWin->vScrollBar,h);
  77.         if (dWin->editOffset < 64000L)
  78.             SetCtlValue(dWin->vScrollBar,(short) ((dWin->editOffset*h)/limit));
  79.         else
  80.             SetCtlValue(dWin->vScrollBar,(short) (dWin->editOffset / (limit/h)));
  81.     }
  82.     else {
  83.         SetCtlMax(dWin->vScrollBar,0);
  84.         SetCtlValue(dWin->vScrollBar,0);
  85.     }
  86.         
  87.     SetPort(savePort);
  88. }
  89.  
  90. //
  91. // Callback routine to handle arrows and page up, page down
  92. //
  93. EditWindowPtr    gDWin;
  94.  
  95. pascal void MyScrollAction(ControlHandle theControl, short thePart)
  96. {
  97.     long        curPos,newPos;
  98.     short        pageWidth;
  99.     Rect        myRect;
  100.     WindowPtr    gp = (WindowPtr) gDWin;
  101.  
  102.     curPos = gDWin->editOffset;
  103.     newPos = curPos;
  104.  
  105.     myRect = ((WindowPtr) gDWin)->portRect;
  106.     myRect.right -= SBarSize-1;
  107.     myRect.bottom -= SBarSize-1;
  108.  
  109.     pageWidth = (gDWin->linesPerPage-1)*16;
  110.  
  111.     switch (thePart) {
  112.       case inUpButton:        newPos = curPos - 16;            break;
  113.       case inDownButton:    newPos = curPos + 16;            break;
  114.         case inPageUp:        newPos = curPos - pageWidth;    break;
  115.       case inPageDown:        newPos = curPos + pageWidth;    break;
  116.     }
  117.  
  118.     if (newPos != curPos) {
  119.         HEditScrollToPosition(gDWin, newPos);
  120.     }
  121. }
  122.  
  123. // Intercept Handler for scroll bars
  124. // Returns true if user clicked on scroll bar
  125. //
  126.  
  127. Boolean HandleControlClick(WindowPtr theWin, Point where)
  128. {
  129.     short             ctlPart;
  130.     ControlHandle    whichControl;
  131.     short            vPos;
  132.     EditWindowPtr    dWin = (EditWindowPtr) theWin;
  133.     ProcPtr            trackActionProc;
  134.     
  135.     // Check if user clicked on scrollbar
  136.     //
  137.     if ((ctlPart = FindControl(where,theWin,&whichControl)) == 0)
  138.         return false;
  139.  
  140.     // Identify window for callback procedure
  141.     //
  142.     gDWin = dWin;
  143.  
  144.     // Use default behavior for thumb
  145.     // Program will crash if you don't!!
  146.     //
  147.     if (ctlPart == inThumb)
  148.         trackActionProc = 0L;
  149.     else
  150.         trackActionProc = MyScrollAction;
  151.  
  152.     // Perform scrollbar tracking
  153.     //
  154.     if ((ctlPart = TrackControl(whichControl, where, trackActionProc)) == 0)
  155.         return false;
  156.  
  157.     if (ctlPart == inThumb) {
  158.         long    newPos,h,limit;
  159.         vPos = GetCtlValue(dWin->vScrollBar);
  160.         h = theWin->portRect.bottom - theWin->portRect.top - (GrowIconSize - 1);
  161.         limit = ((dWin->fileSize+15) & 0xFFFFFFF0) - (dWin->linesPerPage << 4);
  162.         if (vPos == h)
  163.             newPos = ((dWin->fileSize+15) & 0xFFFFFFF0) - (dWin->linesPerPage << 4);
  164.         else if (limit < 64000L)        // JAB 12/10 Prevent Overflow in Calcuation
  165.             newPos = (vPos*limit)/h;
  166.         else
  167.             newPos = vPos*(limit/h);
  168.         newPos -= newPos & 0x0F;
  169.         HEditScrollToPosition(dWin, newPos);
  170.     }
  171.  
  172.     return true;
  173. }
  174.  
  175. // Scroll to an explicit position
  176. //
  177. void HEditScrollToPosition(EditWindowPtr dWin, long newPos)
  178. {
  179.     long    limit;
  180.  
  181.     SetPort((WindowPtr) dWin);
  182.  
  183.     // Constrain scrolling position to legal limits
  184.     //
  185.     limit = ((dWin->fileSize+15) & 0xFFFFFFF0) - (dWin->linesPerPage << 4);
  186.     if (newPos > limit)
  187.         newPos = limit;
  188.     if (newPos < 0)
  189.         newPos = 0;
  190.     dWin->editOffset = newPos;
  191.     
  192.     // Adjust Scrollbars
  193.     AdjustScrollBars((WindowPtr) dWin, false);
  194.  
  195.     // 12/10/93 - Optimize Drawing
  196.     SetCurrentChunk(dWin, dWin->editOffset);
  197.  
  198.     DrawPage(dWin);
  199.     UpdateOnscreen((WindowPtr) dWin);
  200. }
  201.  
  202. void AutoScroll(EditWindowPtr dWin, Point pos)
  203. {
  204.     short    offset;
  205.     if (pos.v < HeaderHeight+TopMargin)
  206.         offset = -16;
  207.     else if (pos.v >= HeaderHeight+TopMargin+dWin->linesPerPage*LineHeight)
  208.         offset = 16;
  209.     else
  210.         return;
  211.     HEditScrollToPosition(dWin, dWin->editOffset+offset);
  212. }